#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   cargoNameIdentify.py    
@Contact :   fengfeng.qiu@amh-group.com

@Modify Time      @Author    
------------      -------    
2022/1.csv/19 17:49   qiufengfeng
'''

from locust import HttpUser,task,between
import json
def load_test_data():
    file_path = '../data/cargo_name_identify_old.txt'
    data = set()

    with open(file_path,'r',encoding='utf-8') as fread:
        for index,line in enumerate(fread):
            if 'input_sentence:' in line:
                if 'cargoId' in line:
                    continue
                try:
                    input_json = json.loads(line.strip().replace("input_sentence: ","").replace("'","\""))
                    if 'cargoName' in input_json:
                        data.add(input_json['cargoName'].replace("\n",""))

                except Exception as e:
                    continue

    return list(data)

cargo_texts = load_test_data()

class HelloWorldUser(HttpUser):
    wait_time = between(0.5, 1)
    host = "http://qa-beetle.amh-group.com"

    @task
    def hello_word(self):
        import random

        data = {"cargoName":random.choice(cargo_texts)}
        header = {"Content-Type": "application/json"}
        import json
        req = self.client.post("/beetle/cargo_name/cargo_name_identify",data=json.dumps(data),headers=header)
        print(req.json())

